import query from '@dfeidao/atom-nodejs/db/query';
import { systemid, productid } from '../atom/config';

interface Message {
	filter: {
		commodity_no: string
	};
	page_no: number;
}

// interface ICommoditySale { 	// 上架商品
// 	commodity_no: string;	// 商品编号
// 	commodity_sale_no: string;	// 上架商品编号
// }
interface IProductImageClassification { // 	商品图片分类
	product_image_class_no: string;
}

interface ICommPicture {
	pic_path: string;
	bstype: string;
	preview_pic: string;
}
interface IP {
	pic_path: string;
	word_text: string;
}
interface ICommoditySale { 	// 上架商品commodity_sale
	commodity_no: string;	// 商品编号
	commodity_sale_no: string;	// 上架商品编号
	category_num: string;	// 类别编号
	category_name: string;	// 类别名称;
	thumbnail: string;	// 缩略图
	pic_type: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string) {
	// const { exec, prepare } = query(action_id, session_id, systemid);
	const { exec: e1, prepare: p1 } = query(action_id, session_id, systemid);
	const { exec: e2, prepare: p2 } = query(action_id, session_id, systemid);
	const { exec: e3, prepare: p3 } = query(action_id, session_id, systemid);
	// commodity_sale_no

	// prepare('commodity_sale', ['commodity_no'], { productid, commodity_sale_no: msg.filter.commodity_sale_no }, 1, 1, [], []);
	// const [commodity_sale] = await exec<ICommoditySale>();
	const commodity_no = msg.filter.commodity_no;

	p3('product_information', ['commodity_no', 'pic_type'], {
		commodity_no, productid
	}, 200, 1, ['create_time'], []);
	const [sale] = await e3<ICommoditySale>();
	if (sale.length === 0) {
		return {
			code: -1,
			msg: '没有找到商品'
		};
	}
	const pic_type = sale[0].pic_type;

	// product_image_classification	商品图片分类
	p1('product_image_classification', ['product_image_class_no'], { productid, commodity_no }, 1, 1, [], []);
	const [product_image_class_no_s] = await e1<IProductImageClassification>();

	const product_image_class_no = product_image_class_no_s[0].product_image_class_no;

	// comm_picture 商品图片
	p2('comm_picture', [
		'pic_path', 'bstype', 'preview_pic'
	], { product_image_class_no, productid, bstype: 'video' }, 200, 1, ['fzno'], []);
	p2('comm_picture', [
		'pic_path', 'bstype', 'preview_pic'
	], { product_image_class_no, productid, bstype: 'picture' }, 200, 1, ['fzno'], []);
	p2('product_description_picture', [
		'pic_path', 'word_text'
	], { product_image_class_no, productid, commodity_no }, 200, 1, ['fzno'], []);
	const [video_s, comm_picture_s, pictures] = await e2<ICommPicture, ICommPicture, IP>();
	const p = pictures.filter((i) => {
		return i.pic_path || i.word_text;
	});
	const bander = video_s.concat(comm_picture_s);
	// return pic_type;
	return { pic_type, bander, pictures: p };
}
